import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Layout from '@/views/layout'
import Article from '@/views/article'
import store from '@/store'
import Operation from '@/views/operation'
Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    {
     path:'/',
     component:Layout,
     redirect:'/article',
     children:[
      {
        path:'/article',
        component:Article
      },
      {
        path:'/operation',
        component:Operation
      }
     ]
    },
    {
      path:'/login',
      component:Login
    }
  ]
})

const whiteList = ['/login']// 设置白名单

router.beforeEach((to, from, next) => {
  const { token } = store.state.user
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      // 如果没有token跳转到登录页
      next('/login')
    }
  }
})

export default router
